package com.hx.er.action.admin.rest;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.LanguageBean;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.base.UserBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminRestService;

public class AdminRestEditAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String SERVER_NAME_UNIQUE_MSG = "admin.restList.message.add.unique.serverName";
	public static final String SERVER_NAME_EMPTY_MSG = "admin.restList.message.add.empty.serverName";
	public static final String REST_NAME_EMPTY_MSG = "admin.restList.message.add.empty.restName";
	public static final String EXP_DATE_EMPTY_MSG = "admin.restList.message.add.empty.expirationDate";
	public static final String EXP_DATE_EXPR_MSG = "admin.restList.message.add.expr.expirationDate";

	private long restId;
	private RestraurantBean restBean;
	private UserBean userBeanPage;

	private String updateUserNickName;
	private String expirationDate;

	private List<LanguageBean> langList;

	@Autowired
	private IAdminRestService adminRestService;

	public String init() {
		this.initPage();
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			return INPUT;
		}

		// 设置过期日期
		try {
			this.restBean.setExpirationDate(StringUtil.parseDate(this.expirationDate, ErProperties.getProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT)));
		} catch (ParseException e) {
			this.addFieldError("msg.expirationDate", super.getText(EXP_DATE_EXPR_MSG));
			return INPUT;
		}

		this.restBean.setRestId(this.restId);
		this.restBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		int count = this.adminRestService.updateRest(this.restBean);

		if (count == 0) {
			this.initPage();
			this.addActionError(super.getText(EDIT_FAILURE_MSG));
			return INPUT;
		} else {
			this.addActionMessage(super.getText(EDIT_SUCCESS_MSG));
			return SUCCESS;
		}
	}

	private void initPage() {
		// 查询餐馆信息
		this.restBean = this.adminRestService.getRestById(this.restId);
		// 最后更新人查询
		UserBean userBean = this.adminRestService.getUserAliveById(this.restBean.getUpdateUserId());
		this.updateUserNickName = userBean.getNickName();
		this.expirationDate = StringUtil.formatDateStr(this.restBean.getExpirationDate(), ErProperties.getProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT));

		// 查询餐厅经理，此处查询编号最小的，也就是录入餐厅是自带的经理
		this.userBeanPage = this.adminRestService.getManagerByRestId(this.restId);

		// 初始化语言
		this.langList = ErLanguages.getLangList();
	}

	private boolean checkParams() {
		// 餐厅名验证
		if (StringUtil.isEmpty(this.restBean.getRestName())) {
			this.addFieldError("msg.restName", super.getText(REST_NAME_EMPTY_MSG));
		}

		// 域名验证
		if (StringUtil.isEmpty(this.restBean.getServerName())) {
			this.addFieldError("msg.serverName", super.getText(SERVER_NAME_EMPTY_MSG));
		}

		// 过期时间验证
		if (StringUtil.isEmpty(this.getExpirationDate())) {
			this.addFieldError("msg.expirationDate", super.getText(EXP_DATE_EMPTY_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public long getRestId() {
		return restId;
	}

	public void setRestId(long restId) {
		this.restId = restId;
	}

	public RestraurantBean getRestBean() {
		return restBean;
	}

	public void setRestBean(RestraurantBean restBean) {
		this.restBean = restBean;
	}

	public String getUpdateUserNickName() {
		return updateUserNickName;
	}

	public String getExpirationDate() {
		return expirationDate;
	}

	public void setExpirationDate(String expirationDate) {
		this.expirationDate = expirationDate;
	}

	public void setUpdateUserNickName(String updateUserNickName) {
		this.updateUserNickName = updateUserNickName;
	}

	public List<LanguageBean> getLangList() {
		return langList;
	}

	public UserBean getUserBeanPage() {
		return userBeanPage;
	}

	public void setUserBeanPage(UserBean userBeanPage) {
		this.userBeanPage = userBeanPage;
	}

}
